package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catalog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author LYJ
 * @Description
 * @date 2021 年 01 月 22 日 17:30
 */
@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;


    @ResponseBody
    @RequestMapping("/index/catalog.json")
    public Map<String, List<Catalog2Vo>> getCatalogJson() {
        //最大的返回对象是一个json对象，说明他是一个map，map就是一个json对象，所以返回类型为一个map
        //不能写Vo，因为他的key都不确定，
        Map<String, List<Catalog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }

    @GetMapping({"/", "index.html"})
    public String indexPage(Model model) {
        //TODO 1、查出所有的一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Category();
        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    @GetMapping("/test")
    public String test() {
        return "test";
    }
}
